package cn.join.ideer.common.api;

import cn.join.ideer.common.exceptions.ApiException;
import cn.join.ideer.common.utils.ObjectUtils;


import java.util.function.Supplier;

/**
 * @author shinyiqi
 * @date 2023/3/5 0:00
 */
public class CommonController {

    public <M> AjaxResult process(Supplier<M> sp) {
        try {
            M val = sp.get();
            if (ObjectUtils.isEmpty(val)) {
                return AjaxResult.error();
            }
            return AjaxResult.success(val);
        } catch (ApiException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            if (e.getMessage().length() >= 20) {
                return AjaxResult.error("系统错误 请联系管理人员");
            }
            return AjaxResult.error(e.getMessage());
        }
    }
}
